
package com.zhihuishu.micro.course.openapi.course.impl;

import com.zhihuishu.micro.course.dto.CourseDataDto;
import com.zhihuishu.micro.course.openapi.course.CourseDataOpenService;
import com.zhihuishu.micro.course.openapi.course.dto.CourseDataOpenDto;
import com.zhihuishu.micro.course.service.CourseDataService;
import com.zhihuishu.remote.RemoteException;
import com.zhihuishu.remote.RemoteResult;
import com.zhihuishu.toolkit.helper.BeanHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@Service("courseDataOpenService")
public class CourseDataOpenServiceImpl implements CourseDataOpenService {
	
	private Logger log = LoggerFactory.getLogger(CourseDataOpenServiceImpl.class);
	
	@Resource
	private CourseDataService courseDataService;

    @Override
    public RemoteResult<List<CourseDataOpenDto>> searchCourseDataOpenLists(Integer bbsId) throws RemoteException {
        return null;
    }

    // 保存资料信息
	@Override
	public RemoteResult<CourseDataOpenDto> saveCourseDataOpen(CourseDataOpenDto courseDataOpenDto) throws RemoteException {
		
		RemoteResult<CourseDataOpenDto> result=new RemoteResult<CourseDataOpenDto>();
		if(courseDataOpenDto == null){
			result.setSuccess(false);
			result.setResult(null);
		}		
		try {
			
			CourseDataDto courseDataDto = BeanHelper.transTo(courseDataOpenDto, CourseDataDto.class);
			courseDataDto = this.courseDataService.insertCourseData(courseDataDto);
			CourseDataOpenDto courseDataOpenDt = BeanHelper.transTo(courseDataDto, CourseDataOpenDto.class);
			result.setSuccess(true);
			result.setResult(courseDataOpenDt);
		} catch (Exception e) {
			log.error("保存资料信息报错");
			throw new RemoteException(e).setMessage("保存资料信息报错!");
		}
		return result;
	}
    //查询资料列表
	@Override
	public RemoteResult<List<CourseDataOpenDto>> searcherCourseDataOpenLists(Set<Integer> ids) throws RemoteException {
		RemoteResult<List<CourseDataOpenDto>> result = new RemoteResult<List<CourseDataOpenDto>>();
		
		if(CollectionUtils.isEmpty(ids)){
			result.setSuccess(false);
			result.setResult(null);
		}
		try {
			List<CourseDataDto> courseDataDtos = courseDataService.findCourseDataLists(ids);
			List<CourseDataOpenDto> courseDataOpenDtos = new ArrayList<CourseDataOpenDto>();
			if(!CollectionUtils.isEmpty(courseDataDtos));
			for(CourseDataDto courseDataDto : courseDataDtos){
				CourseDataOpenDto courseDataOpenDto = BeanHelper.transTo(courseDataDto, CourseDataOpenDto.class);
				courseDataOpenDtos.add(courseDataOpenDto);
			}
			result.setSuccess(true);
			result.setResult(courseDataOpenDtos);
		} catch (Exception e) {
			
			throw new RemoteException(e).setMessage("查询资料信息报错!"); 
		}
		return result;
	}
    //删除资料列表（逻辑上的删除）
	@Override
	public RemoteResult<Void> deleteCourseDataOpen(Integer id) throws RemoteException {
		
		RemoteResult<Void> result = new RemoteResult<Void>();
		
		if(id == null){
			result.setSuccess(false);
			result.setResult(null);
		}
		try {
			this.courseDataService.deleteCourseData(id);
			result.setSuccess(true);
			result.setResult(null);
		} catch (Exception e) {
			throw new RemoteException(e).setMessage("删除资料出错!");
		}
		return result;
	}
    //根据id查询资料信息
	@Override
	public RemoteResult<CourseDataOpenDto> searchCourseDataOpen(Integer id) throws RemoteException {
		RemoteResult<CourseDataOpenDto> result =  new RemoteResult<CourseDataOpenDto>();
		if(id == null){
			result.setSuccess(true);
			result.setResult(null);
		}
		try {
			CourseDataDto courseDataDto= this.courseDataService.findCourseData(id);
			CourseDataOpenDto courseDataOpenDto = BeanHelper.transTo(courseDataDto, CourseDataOpenDto.class);
			result.setSuccess(true);
			result.setResult(courseDataOpenDto);
		} catch (Exception e) {
			throw new RemoteException(e).setMessage("根据id查询资料出错!");
		}
		return result;
	}

}
